package com.chen.controller;

import com.chen.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * 百度登录
 *
 * @author chenchao
 * @date 2022/10/23 21:45
 */

@Controller
@Slf4j
@Api(value = "BaiduLoginController", tags = "百度登录")
@RequestMapping(value = "/baidu")
@SuppressWarnings("all")
public class BaiduLoginController {

    //    回调地址
    @Value("${baidu.redirectUri}")
    private String redirectUri;

    //    appKey
    @Value("${baidu.appKey}")
    private String appKey;

    //    私钥
    @Value("${baidu.appSecretKey}")
    private String appSecretKey;

    //    appId
    @Value("${baidu.appId}")
    private String appId;


    /**
     * 百度url
     *
     * @return
     */
    @ApiOperation("获取百度登录页面")
    @GetMapping("/oauth/url")
    @ResponseBody
    public Result baidu(HttpSession session) {
//    用于第三方应用CSRF攻击
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        session.setAttribute("state",uuid);

//        获取跳转url
        String url="https://openapi.baidu.com/oauth/2.0/authorize?response_type=code"+
                "&client_id="+appKey+"&redirect_uri="+ URLEncoder.encode(redirectUri)+
                "&state="+uuid+"&qrcode=1"+"&display=popup";
        return Result.success(url);
    }



}
